﻿using System;
using System.IO;
using NguyenHoa.Domains;
using NguyenHoa.Services;
using NguyenHoa.Web.App_Code;

namespace NguyenHoa.Web.Controls
{
  public partial class AdsControl : BaseControl
  {
    protected string m_Advertisement = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
      m_Advertisement = LoadAdvertise();
    }

    private string LoadAdvertise()
    {
      TList<Domains.Advertisement> advList = new AdvertisementService().GetByType(AdvertisementType.Advertisement, true);
      string html = string.Empty;
      if (advList != null)
      {
        foreach (Domains.Advertisement adv in advList)
        {
          if (!string.IsNullOrEmpty(adv.ImagePath) && File.Exists(MapPath("../" + adv.ImagePath)))
          {
            string fileType = adv.ImagePath.Substring(adv.ImagePath.LastIndexOf(".") + 1);
            if (fileType == "swf")
            {
              html += "<div>";
              html += Utils.EmbedObject(MapPath("../" + adv.ImagePath), "../" + adv.ImagePath, GlobalCache.Instance.AdvertiseFlashWidth.ToString(), string.Empty, "flash_" + adv.Id);
              html += "</div>";
            }
            else
            {
              html += "<div>";
              html += "<a style='cursor:pointer;' href='" + adv.Website + "' target='_blank'>";
              html += "<img alt='' style='width: 190px;' src='../" + adv.ImagePath + "'>";
              html += "</img>";
              html += "</a>";
              html += "</div>";
            }
          }
        }
      }
      return html;
    }
  }
}